#include "IdentificatorTable.hpp"

#include "Error.hpp"

IdentificatorTable::IdentificatorTable () :
    _nextIndex ( 0 )
{
}

IdentificatorTable::~IdentificatorTable ()
{
}

unsigned int
IdentificatorTable::addIdentificator ( const std::string& name )
{
    if ( _nameToIndex.find ( name ) != _nameToIndex.end () )
        return _nameToIndex [ name ];
    unsigned int index = _nextIndex ++;
    _nameToIndex [ name ] = index;
    _indexToName [ index ] = name;
    return index;
}

const std::string&
IdentificatorTable::getNameOf ( const unsigned int index ) const
{
    if ( _indexToName.find ( index ) == _indexToName.end () )
        throw Error ( IDENTIFICATORTABLE_UNASSIGNED_INDEX, "this index isn't assigned to any identificator", IDENTIFICATORTABLE_LEVEL_ERROR );
    return _indexToName.find ( index )->second;
}

const unsigned int
IdentificatorTable::getIndexOf ( const std::string& name ) const
{
    if ( _nameToIndex.find ( name ) == _nameToIndex.end () )
        throw Error ( IDENTIFICATORTABLE_UNASSIGNED_INDEX, "this index isn't assigned to any identificator", IDENTIFICATORTABLE_LEVEL_ERROR );
    return _nameToIndex.find ( name )->second;
}
